<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';
// 上线初始化 一次  商品热度值

use Biz\cli\IniData\GoodsActionCounterFun;
use Biz\model\GoodsActionCount;
use Biz\model\GoodsHeatDegree;

$currentDate = date("Y-m-d");
$dateNums = range(30, 0);

foreach ($dateNums as $dateNum) {
	$dateRange = [
		date("Y-m-d",strtotime($currentDate) - (86400 * ($dateNum + 1))),
		date("Y-m-d",strtotime($currentDate) - (86400 * $dateNum))
	];

	GoodsActionCounterFun::InitData($dateRange);
}


///初始化当前的商品 热度 范围 30 天内
$goodsId = 0;
//$typeBaseMap = [
//	GoodsActionCount::DEGREE_TYPE_BROWSE => GoodsHeatDegree::DEGREE_BASE_BROWSE,
//	GoodsActionCount::DEGREE_TYPE_FAV => GoodsHeatDegree::DEGREE_BASE_FAV,
//	GoodsActionCount::DEGREE_TYPE_ADD_CART => GoodsHeatDegree::DEGREE_BASE_ADD_CART,
//	GoodsActionCount::DEGREE_TYPE_ORDER => GoodsHeatDegree::DEGREE_BASE_ORDER,
//
//];

$info = GoodsActionCount::get('*', [
	'goods_id[>]' => $goodsId,
	'ORDER' => ['goods_id' => 'ASC']
]);

while ($info) {
	$goodsId = $info['goods_id'];

	initHeatDegree($goodsId);

	$info = GoodsActionCount::get('*', [
		'goods_id[>]' => $goodsId,
		'ORDER' => ['goods_id' => 'ASC']
	]);
}



function initHeatDegree($goodsId) {
	global $currentDate;
	$heatDegree = [];
	$counterInfos = GoodsActionCount::select('*', [
		'goods_id' => $goodsId,
		'status' => GoodsActionCount::STAUTS_NORMAL
	]);

	foreach ($counterInfos as $counterInfo) {
		$heatDegreeOnType = $counterInfo['action_num'] * (32 - (strtotime($currentDate) - strtotime($counterInfo['add_date'])) / 86400) * GoodsHeatDegree::ACTION_COUNT_HEAT_MAP[$counterInfo['type']];
		$heatDegree[$counterInfo['type']] = isset($heatDegree[$counterInfo['type']]) ? $heatDegree[$counterInfo['type']] + $heatDegreeOnType : $heatDegreeOnType;
	}

	GoodsHeatDegree::insert([
		'goods_id' => $goodsId,
		'heat_degree' => array_sum($heatDegree),
		'add_date' => date("Y-m-d",strtotime($currentDate) - 86400),
		'status' => GoodsHeatDegree::STAUTS_NORMAL
	]);

}

